﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace MINERP.DAL.InfoConfig
{
    public  class AreaDAL:DALBase
    {
        private static ObjectQuery<GridSubArea> GetAreas()
        {
            return DataEntites.GridSubArea;
        }

        /// <summary>
        /// Get Area list
        /// </summary>
        /// <returns></returns>
        public static List<GridSubArea> GetAreaList()
        {
            List<GridSubArea> list = new List<GridSubArea>();
            foreach (var item in GetAreas())
            {
                list.Add(item);
            }
            return list;
        }
        /// <summary>
        /// Get Area object
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static GridSubArea GetAreaObj(int id)
        {
            return DataEntites.GridSubArea.First(a => a.SubAreaID == id);
        }
        public static GridSubArea GetAreaObj(string name)
        {
            return DataEntites.GridSubArea.First(a => a.SubAreaName == name);
        }
        /// <summary>
        /// Add GridSubArea
        /// </summary>
        /// <param name="obj"></param>
        public static void Add(GridSubArea obj)
        {
            DataEntites.AddToGridSubArea(obj);
            DataEntites.SaveChanges();
        }
        /// <summary>
        ///  Update GridSubArea
        /// </summary>
        public static void Update()
        {
            DataEntites.SaveChanges();
        }
        /// <summary> 
        /// Delete GridSubArea
        /// </summary>
        /// <param name="obj"></param>
        public static void Delete(GridSubArea obj)
        {
            DataEntites.DeleteObject(obj);

            try
            {
                DataEntites.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        public static void DeleteGrid(GridSubArea area)
        {
            List<Grid> gridList = new List<Grid>();
            if (!area.Grid.IsLoaded)
            {
                area.Grid.Load();
            }
            gridList = area.Grid.ToList();
            foreach (Grid grid in gridList)
            {
                //DeleteGridRecord(grid);
                DataEntites.DeleteObject(grid);
            }
            DataEntites.SaveChanges();
        }
        public static void DeleteGridRecord(Grid grid)
        {
            grid.GridRecord.Load();
            List<GridRecord> gridRecords = grid.GridRecord.ToList();
            foreach (GridRecord record in gridRecords)
            {

                DataEntites.DeleteObject(record);
            }
 
        }
    }
}
